package com.five.matebook.activity.income;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.five.matebook.R;
import com.five.matebook.activity.db.IncomeRecordTable;
import com.five.matebook.activity.db.OneQueryData;
import com.five.matebook.activity.db.QueryFilter;
import com.five.matebook.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class IncomeLsitFragment extends Fragment {
    private static final String TAG = IncomeLsitFragment.class.getSimpleName();

    private volatile boolean mNeedUpdateOnResume = false;
    public IncomeLsitFragment(List<OneQueryData> data) {
        this.mData = data;
        if (this.mData == null) {
            this.mData = new ArrayList<>();
        }
        mNeedUpdateOnResume = data == null;
    }

    public IncomeLsitFragment() {
        mNeedUpdateOnResume = true;
    }

    private ListView listView;
    private IncomeRecordTable mTable;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_income, container, false);
        listView = v.findViewById(R.id.listview);
        mAdapter = new IncomeLsitFragment.MyListViewAdapter();
        listView.setAdapter(mAdapter);
        mTable = IncomeRecordTable.getInstance(getContext());

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getContext(), IncomeRecordActivity.class);
                OneQueryData d = (OneQueryData) mAdapter.getItem(position);
                intent.putExtra("id", d.getLong("id", 0));
                startActivity(intent);
            }
        });

        filter = new QueryFilter.Builder()
                .setOrderBy(IncomeColume.UPDATE_TIME, true)
                .setLimit(1000)
                .build();

        update();
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        update();
    }

    private List<OneQueryData> mData = new ArrayList<>();
    private BaseAdapter mAdapter;
    private QueryFilter filter;
    private void update() {
        if (mNeedUpdateOnResume) {
            mData = mTable.query(filter);
        }
        mAdapter.notifyDataSetChanged();
    }

    class ViewHolder {
        TextView txtUsage;
        TextView txtSpentRole;
        TextView txtSpentTime;
        TextView txtSpentMoney;
        ImageView imgAccount;
    }

    class MyListViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_income, null);
                holder = new ViewHolder();
                holder.imgAccount = convertView.findViewById(R.id.icon);
                holder.txtUsage = convertView.findViewById(R.id.txt_usage);
                holder.txtSpentMoney = convertView.findViewById(R.id.txt_cost);
                holder.txtSpentRole = convertView.findViewById(R.id.txt_spentRole);
                holder.txtSpentTime = convertView.findViewById(R.id.txt_spentTime);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            OneQueryData data = (OneQueryData) getItem(position);
            holder.txtSpentMoney.setText(String.format("+%.2f", data.getFloat(IncomeColume.SPENT_MONEY, 0)));
            String usage = data.get(IncomeColume.SPENT_USAGE) + " - " + data.get(IncomeColume.SPENT_SENCOND_USAGE);
            holder.txtUsage.setText(usage);
            String role = data.get(IncomeColume.INCOME_ROLE);
            String remarks = data.get(IncomeColume.SPENT_REMARKS);
            String temp = role;
            if (!TextUtils.isEmpty(remarks)) {
                temp += " ~ " + remarks.substring(0, Math.min(remarks.length(), 13)) + "..";
            }
            holder.txtSpentRole.setText(temp);
            String hanppen = DateUtils.toString(data.getLong(IncomeColume.HAPPEND_TIME, 0), false);
            holder.txtSpentTime.setText(hanppen);
            holder.imgAccount.setImageResource(R.drawable.income);

            if (hanppen.contains("本月")) {
                convertView.setBackgroundColor(getResources().getColor(R.color.this_month));
            } else {
                convertView.setBackgroundColor(Color.TRANSPARENT);
            }
            return convertView;
        }
    }
}
